import { plugin, AMessage } from '../api/api'
import { _path, _dirname, _item, fs, Write_player } from '../mode/allapi'
export class manage extends plugin {
  constructor() {
    super({
      dsc: '资源',
      rule: [
        {
          reg: /^(#|\/)?发(.)*$/,
          fnc: 'recoup'
        }
      ]
    })
  }
  async recoup(e: AMessage) {
    //发全体的
    const id = e.user_id
    const mapname = ['11111'] //更改此处id设置管理
    if (!mapname.includes(id)) return false
    const didian = e.msg.replace(/(#|\/)?发/, '')
    const [thing_data, thing_shu] = didian.split('*')
    const date = {
      石头: 'stone_num',
      木材: 'wood_num',
      铁矿: 'iron_num',
      五铢钱: 'currency_num'
    }
    const playerList = fs
      .readdirSync(_dirname + '/resources/data/player')
      .filter(file => file.endsWith('.json'))
      .map(file => {
        const playerData = JSON.parse(
          fs.readFileSync(_dirname + `/resources/data/player/${file}`, 'utf-8')
        )
        return {
          data: playerData,
          id: playerData.user_id
        }
      })
    if (!playerList || parseInt(thing_shu) == 0) return false
    for (let i = 0; i < playerList.length; i++) {
      playerList[i].data[date[thing_data]] += Number(thing_shu)
      await Write_player(playerList[i].id, playerList[i].data)
      e.reply('补偿成功')
    }
  }
}
